package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.po.LessonPlan;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-07
 */
@RestController
@RequestMapping("/lessons")
@Api(tags = "我的课程相关接口")
@RequiredArgsConstructor
public class LearningLessonController {

    private final ILearningLessonService learningLessonService;


    @GetMapping("page")
    @ApiOperation("分页查询我的课表")
    public PageDTO<LearningLessonVO> queryMyLessons(PageQuery pageQuery){
        return learningLessonService.queryMyLessons(pageQuery);
    }

    @GetMapping("now")
    @ApiOperation("查询正在学习的课程")
    public LearningLessonVO queryMyCurrentLesson(){
        return learningLessonService.queryMyCurrentLesson();
    }


    @DeleteMapping("/{courseId}")
    @ApiOperation("删除订单")
    public void deleteInvalidCourse(@PathVariable Long courseId){
        List<Long> courseIds = new ArrayList<>();
        courseIds.add(courseId);
        Long user = UserContext.getUser();
        learningLessonService.deleteLessons(user,courseIds);
    }
    @ApiOperation("校验课程是否有效")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(@PathVariable("courseId") Long courseId){
     return learningLessonService.isCourseValid(courseId);
    }

    @GetMapping("{courseId}")
    @ApiOperation("查看用户课表中指定课程状态")
    public LearningLessonVO getByIdStatus(@PathVariable Long courseId) {
        return learningLessonService.getByIdStatus(courseId);
    }


    @GetMapping("/{courseId}/count")
    @ApiOperation("统计课程学习人数")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId) {
        return learningLessonService.countLearningLessonByCourse(courseId);
    }

    @PostMapping("/plans")
    @ApiOperation("创建学习计划")
    public void createPlan(@RequestBody LessonPlan lesson){
        learningLessonService.createPlan(lesson);
    }


    @GetMapping("/plans")
    @ApiOperation("查询我的学习计划")
    public LearningPlanPageVO queryMyPlans(PageQuery pageQuery){
        return learningLessonService.queryMyPlans(pageQuery);
    }
}
